package cn.iocoder.yudao.module.member.controller.app.email;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.member.controller.app.email.vo.AppEmailRespVO;
import cn.iocoder.yudao.module.member.controller.app.email.vo.AppEmailSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.email.EmailDO;
import cn.iocoder.yudao.module.member.service.email.EmailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 App - 用户常用邮箱")
@RestController
@RequestMapping("/member/email")
@Validated
public class AppEmailController {

    @Resource
    private EmailService emailService;

    @PostMapping("/create")
    @Operation(summary = "创建用户常用邮箱")
    @PreAuthenticated
    public CommonResult<Long> createEmail(@Valid @RequestBody AppEmailSaveReqVO createReqVO) {
        final Long loginUserId = getLoginUserId();
        return success(emailService.createEmailByUser(loginUserId, createReqVO));
    }

    @GetMapping("/list")
    @Operation(summary = "获得用户常用邮箱分页")
    @PreAuthenticated
    public CommonResult<List<AppEmailRespVO>> getEmailList() {
        final Long loginUserId = getLoginUserId();
        List<EmailDO> pageResult = emailService.getEmailOrderByUpdateTime(loginUserId, 20);
        return success(BeanUtils.toBean(pageResult, AppEmailRespVO.class));
    }

}